package com.zzyl.framework.interceptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zzyl.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import javax.naming.directory.SearchControls;
import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /****
     * 新增填充
     * @param metaObject
     */

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "createBy", Long.class, getUserId());
    }


    /****
     * 修改填充
     * @param metaObject
     */

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateBy", Long.class, getUserId());
    }

    public static Long getUserId(){
        Long userId = SecurityUtils.getUserId();
        if (userId != null){
            return userId;
        }
        return 1L;
    }
}
